<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th><input type="checkbox" id="th_check"></th>
                <th>商品</th>
                <th>价钱</th>
            </tr>
        </thead>
        <tbody id="tb_checks">
            <tr>
                <td><input type="checkbox"></td>
                <td>iPhone8</td>
                <td>8000.00</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>iPad Pro</td>
                <td>5000.00</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>iPad Air</td>
                <td>2000.00</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>Apple wtach</td>
                <td>2000.00</td>
            </tr>
        </tbody>
    </table>
    <script>
        var checkall = document.getElementById('th_check');
        var checkboxs = document.getElementById('tb_checks').getElementsByTagName('input');
        checkall.onclick = function(){
            var flag = this.checked;
            for (var i = 0; i < checkboxs.length; i++){
                // 记住这里的对象不用使用style来.的方法去调用下边的checked,而是直接checked就可以改变其中的值
                checkboxs[i].checked = flag;
            }
        }
        for(var i = 0; i < checkboxs.length; i++){
            checkboxs[i].onclick = function(){
                var newFlag = true;
                for(var i = 0 ; i < checkboxs.length; i++){
                    if (checkboxs[i].checked !== true){
                        newFlag = false;
                        break;
                    }
                }
                checkall.checked = newFlag;
            }
        }
    </script>
</body>
</html>